package com.link.play.fit.ai.ui.guide

import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.link.play.fit.ai.databinding.FgFastingGoalBinding
import com.link.play.fit.ai.ui.guide.ac.GuidePbAc
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch


class FastingGoalFg : BasePageFragment<FgFastingGoalBinding>(FgFastingGoalBinding::inflate) {

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val activity = (requireActivity() as GuidePbAc)
        val data = activity.viewModel.userGuideState

        lifecycleScope.launch {
            data.collectLatest {
                when (it.fastingGoal) {
                    0 -> {
                        binding.v1.visibility = View.VISIBLE
                        binding.v2.visibility = View.INVISIBLE
                        binding.v3.visibility = View.INVISIBLE
                    }

                    1 -> {
                        binding.v1.visibility = View.INVISIBLE
                        binding.v2.visibility = View.VISIBLE
                        binding.v3.visibility = View.INVISIBLE
                    }

                    2 -> {
                        binding.v1.visibility = View.INVISIBLE
                        binding.v2.visibility = View.INVISIBLE
                        binding.v3.visibility = View.VISIBLE
                    }
                }
            }
        }

        binding.card1.setOnClickListener {

            data.update {
                it.copy(fastingGoal = 0)
            }

        }

        binding.card2.setOnClickListener {

            data.update {
                it.copy(fastingGoal = 1)
            }

        }

        binding.card3.setOnClickListener {
            data.update { it.copy(fastingGoal = 2) }
        }

    }
}